'use strict';

const Controller = require('egg').Controller;

const md5 = require('md5-node');
const fs = require('fs');
const dayjs = require('dayjs');
const path = require('path');

class MusicController extends Controller {
    //上传图片
    async upload() {
        let stream = await this.ctx.getFileStream();
        console.log(stream)
        const newTime = new Date();                                          //当前时间
        let fileBasePath = this.config.uploadBaseDir;                        //上传根目录
        let filePath = dayjs(new Date()).format('/YYYYMMDD/');               //上传目录
        let fileName = md5(newTime) +  path.extname(stream.filename);        //文件名
        let filePathName = path.join(filePath, fileName);                    //文件完整路径
        let uploadPath = path.join(fileBasePath, filePathName);              //上传完整路径

        console.log(uploadPath);

        let uploadDir  = path.join(fileBasePath, filePath);

        if (!fs.existsSync(uploadDir))                                       //判断目录是都存在，不存在创建
            fs.mkdirSync(uploadDir, {recursive: true});   //recursive 参数是是否递归创建

        let writerStream = fs.createWriteStream(uploadPath);
        stream.pipe(writerStream);
        writerStream.on('error', function(err) {
            throw err.stack;
        });
        this.ctx.body = {
            code:1,
            url:uploadPath.slice(4)
        }
    }

    //添加歌曲
    async addmusic(){
        let {ctx,service} = this;
        let {name,isup=true,pic,singer} = ctx.request.body;
        if(name && pic && singer){
            try{
                await service.music.addmusic(name,pic,singer,isup);
                ctx.body = {
                    code:1,
                    msg:'添加成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //查询歌曲
    async querymusic(){
        let {ctx,service} = this;

        let {pagenum=1,limit=5} = ctx.query;

        let startIndex = (pagenum-1)*limit;

        let list = await service.music.querymusic(startIndex,limit);

        let total = await service.music.total();

        ctx.body = {
            code:1,
            data:list,
            total:total[0]['count(*)']
        }
    }

    //修改歌曲
    async updatemusic(){
        let {ctx,service} = this;
        let {name,isup=true,pic,singer,id} = ctx.request.body;
        if(name && pic && singer && id){
            try{
                await service.music.updatemusic(name,pic,singer,isup,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //删除歌曲
    async deletemusic(){
        let {ctx,service} = this;
        let {id} = ctx.query;
        if(id){
            try{
                await service.music.deletemusic(id);
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    } 
}

module.exports = MusicController;
